package com.gmom.tako;

import java.util.List;

import com.gmom.tako.db.DbAccessor;
import com.gmom.tako.model.Rss;
import com.gmom.tako.model.Url;


import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

	private static List<Url> url;
	private static UrlAdapter adapter;
	private View progressBar;
	public static String INTENT_URL_URL = "url";
	public static String INTENT_URL_ID = "url_id";
	public static String INTENT_URL_TITLE = "url_title";
	public static String INTENT_RSS = "rss";
		
	class UrlAdapter extends ArrayAdapter<Url> {
		
		public UrlAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			
			Url item = getItem(position);
			if( row == null) {
				//Viewを作成
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.list_row, null);
			}
			
			if(item != null) {
				//title
				TextView textTitle = (TextView)row.findViewById(R.id.text_title);
				if(textTitle != null) {
					textTitle.setText(item.getTitle().toString());
				}
								
			}
			int l = url.size();
			((TextView)row.findViewById(R.id.text_title)).setTextColor(Color.argb(200,(int)(255*Math.random()), (int)(255*Math.random()), (int)(255*Math.random())));
			//row.setBackgroundColor(Color.rgb(255*(position+1)/(l*2), (255*(position+1)/(l*3/2)), 255*(position+1)/l));
			return row;
		}
		
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        progressBar = findViewById(R.id.progressBar1);
		((LinearLayout)findViewById(R.id.LinearLayout1)).removeView(progressBar);
        //DBからデータ取得して、リストで表示
        refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
        //DBからデータ取得して、リストで表示
    	switch (item.getItemId()) {
		case R.id.menu_add_url:
			showUrlAddDialog();
			break;
		default:
			break;
		}
    	return true;
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Intent intent = new Intent(this, RssListActivity.class);
    	
    	//Log.v("mainactivity", "url:"+url);
    	//urlを送る
    	intent.putExtra(INTENT_URL_URL, url.get(position).getUrl());
    	intent.putExtra(INTENT_URL_ID, url.get(position).get_id());
    	intent.putExtra(INTENT_URL_TITLE, url.get(position).getTitle());
    	startActivity(intent);
    }

                	
	//起動時と更新ボタン押したとき呼ばれる
	public void refresh() {
		//DBからのデータを入れる
        adapter = new UrlAdapter(this, 0);
        setListAdapter(adapter);
        
        url = new DbAccessor().getUrlList(this);
        for (Url urls : url) {
			adapter.add(urls);
		}
        //Log.v("mainactivity", "url:"+url);
	}

    public void showUrlAddDialog() {
    	final EditText editView = new EditText(this);
    	new AlertDialog.Builder(MainActivity.this)
        .setIcon(android.R.drawable.ic_dialog_info)
        .setTitle("RSS URL追加")
        .setView(editView)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                new RssUrlAdditionTask(new AsyncTaskProgress() {				
					@Override
					public void onComplete(List<Rss> rss) {
						refresh();
					}
				}, MainActivity.this).execute(editView.getText().toString());
            }
        })
        .setNeutralButton("キャンセル", null)
        .setCancelable(true)
        .show();

    }

}
